package com.singfan.app.controller;

import com.singfan.app.controller.model.HttpModelConverter;
import com.singfan.app.controller.model.request.HairStyleDetailRequest;
import com.singfan.app.controller.model.request.HairStyleRecommendRequest;
import com.singfan.app.controller.model.response.HairStyleDetailResponse;
import com.singfan.app.controller.model.response.HairStyleRecommendResponse;
import com.singfan.app.controller.model.response.ResultCode;
import com.singfan.app.controller.model.response.partial.HairstylePartial;
import com.singfan.app.controller.model.response.partial.LocationPartial;
import com.singfan.app.util.RequestParamChecker;
import com.singfan.common.util.JsonUtil;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.model.Barber;
import com.singfan.core.dao.database.model.BarberHairstyle;
import com.singfan.core.dao.database.model.CustomerToken;
import com.singfan.core.dao.database.model.Shop;
import com.singfan.core.dao.database.model.constant.CollectType;
import com.singfan.core.dao.database.model.constant.FilterType;
import com.singfan.core.dao.database.model.constant.GenderType;
import com.singfan.core.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-08-25 13:50
 * Desc:
 */
@Controller
public class HairstyleController extends SingFanControllerBase {
    @Autowired
    private IShopService shopService;

    @RequestMapping(value="/v1/hairstyle/recommend/list", method= RequestMethod.POST)
    public void recommendList(HttpServletRequest request, HttpServletResponse response){
        HairStyleRecommendResponse result = new HairStyleRecommendResponse();
        HairStyleRecommendRequest hairStyleRecommendRequest = request2Bean(request, HairStyleRecommendRequest.class);

        String ip = getIpAddress(request);
        String accessToken = hairStyleRecommendRequest.getAccessToken();
        int listOrderType = hairStyleRecommendRequest.getListOrderType();
        int cityId = hairStyleRecommendRequest.getCityId();
        int businessCircleId = hairStyleRecommendRequest.getBusinessCircleId();
        int pageIndex = hairStyleRecommendRequest.getPageIndex();
        int pageSize = hairStyleRecommendRequest.getPageSize();
        int gender = hairStyleRecommendRequest.getGender();
        String longitudeStr = hairStyleRecommendRequest.getLongitude();
        String latitudeStr = hairStyleRecommendRequest.getLatitude();

        boolean checkResult = true;
        if (!FilterType.isFilterType(listOrderType)){
            checkResult = false;
        }
        else if (!RequestParamChecker.checkPageIndexAndSize(pageIndex, pageSize)){
            checkResult = false;
        }
        else if (!GenderType.isGenderType(gender)){
            checkResult = false;
        }
        else if(!RequestParamChecker.checkLongitude(longitudeStr)){
            checkResult = false;
        }
        else if(!RequestParamChecker.checkLatitude(latitudeStr)){
            checkResult = false;
        }

        double longitude = Double.parseDouble(longitudeStr);
        double latitude = Double.parseDouble(latitudeStr);

        if (checkResult){
            List<BarberHairstyle> barberHairstyleList = barberService.findBarberHairstyleList(listOrderType, cityId, businessCircleId, gender, longitude, latitude, pageIndex, pageSize);
            List<HairstylePartial> hairstylePartialList = new ArrayList<>(barberHairstyleList.size());
            for(BarberHairstyle barberHairstyle : barberHairstyleList){
                HairstylePartial hairstylePartial = HttpModelConverter.barberHairstyle2HairstylePartial(barberHairstyle);
                hairstylePartialList.add(hairstylePartial);
            }
            result.setNewList(pageIndex == 0);
            result.setHairstyleList(hairstylePartialList);

            traceLog.debug(String.format("HairstyleController.recommendList() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, listOrderType:%4$d, cityId:%5$d, businessCircleId:%6$d, pageIndex:%7$d, pageSize:%8$d, gender:%9$d, longitude:%10$s, latitude:%11$s", ResultCode.SUCCESS, ip, accessToken, listOrderType, cityId, businessCircleId, pageIndex, pageSize, gender, longitude, latitude));
        }
        else {
            result.setResultCode(ResultCode.REQUEST_PARAM_ERROR);

            traceLog.error(String.format("HairstyleController.recommendList() error with REQUEST_PARAM_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, listOrderType:%4$d, cityId:%5$d, businessCircleId:%6$d, pageIndex:%7$d, pageSize:%8$d, gender:%9$d, longitude:%10$s, latitude:%11$s", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, listOrderType, cityId, businessCircleId, pageIndex, pageSize, gender, longitude, latitude));
        }

        bean2Response(response, result);
    }

    @RequestMapping(value="/v1/hairstyle/detail", method= RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String detail(HttpServletRequest request){
        HairStyleDetailResponse result = new HairStyleDetailResponse();
        HairStyleDetailRequest hairStyleDetailRequest = request2Bean(request, HairStyleDetailRequest.class);

        String ip = getIpAddress(request);
        String accessToken = hairStyleDetailRequest.getAccessToken();
        long hairstyleId = hairStyleDetailRequest.getHairstyleID();

        BarberHairstyle barberHairstyle = barberService.findBarberHairstyle(hairstyleId);
        if (barberHairstyle != null){
            Barber barber = barberService.findBarber(barberHairstyle.getBarberId());
            Shop shop = shopService.findShop(barber.getShopId());
            result.setHairstyleID(barberHairstyle.getId());
            result.setHairstyleName(barberHairstyle.getHairstyleName());
            result.setHairstylePrice(barberHairstyle.getPrice());
            result.setHairstyleImgUrlList(StringUtil.split(barberHairstyle.getImgList(), ";"));
            result.setOrderCount(barberHairstyle.getOrderCount());
            long customerId = 0;
            if (!StringUtil.isNullOrEmpty(hairStyleDetailRequest.getAccessToken())){
                CustomerToken customerToken = customerService.findCustomerToken(hairStyleDetailRequest.getAccessToken());
                if (customerToken != null){
                    customerId = customerToken.getCustomerId();
                }
            }

            if (customerId > 0){
                boolean isCollected = customerService.isCollected(customerId, barberHairstyle.getId(), CollectType.BARBER_HAIR);
                result.setCollected(isCollected);
            }
            else {
                result.setCollected(false);
            }
            result.setHairstyleDesc(barberHairstyle.getHairstyleDesc());
            result.setBarberPartial(HttpModelConverter.barber2BarberPartial(barber));
            if (shop != null){
                result.setShopTelNum(shop.getContactNumber());
            }
            LocationPartial locationPartial = new LocationPartial();
            locationPartial.setAddressText(barber.getAddress());
            locationPartial.setShopMasterName(barber.getMasterShopName());
            locationPartial.setShopSlaveName(barber.getSlaveShopName());
            locationPartial.setLongitude(barber.getLongitude().toString());
            locationPartial.setLatitude(barber.getLatitude().toString());
            result.setLocationPartial(locationPartial);

            traceLog.debug(String.format("HairstyleController.detail() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, hairstyleId:%4$d", ResultCode.SUCCESS, ip, accessToken, hairstyleId));
        }
        else {
            result.setResultCode(ResultCode.REQUEST_PARAM_ERROR);
            traceLog.error(String.format("HairstyleController.detail() error with REQUEST_PARAM_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, hairstyleId:%4$d", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, hairstyleId));
        }

        return JsonUtil.bean2Json(result);
    }
}